package com.yylending.las.main.message;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.support.v7.widget.LinearLayoutManager;
import com.yyfax.common.CLog;
import com.yyfax.common.recyclerview.adapter.CommonAdapter;
import com.yyfax.common.recyclerview.adapter.MultiItemTypeAdapter;
import com.yyfax.common.recyclerview.base.ViewHolder;
import com.yyfax.common.recyclerview.listener.OnLoadMoreListener;
import com.yyfax.common.recyclerview.listener.OnRefreshListener;
import com.yyfax.common.recyclerview.view.FooterLoadMoreView;
import com.yyfax.common.recyclerview.view.IRecyclerView;
import com.yyfax.common.recyclerview.wrapper.StatesAdapter;
import com.yyfax.common.recyclerview.wrapper.WrapperAdapter;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yylending.las.R;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.StatIDSet;
import com.yyfax.infrastructure.statistics.StatService;

import java.util.ArrayList;
import java.util.List;

/**
 * 站内信
 */
public class MessageActivity extends BaseActivity implements MessagePresenter.View, StatesAdapter.RetryClickListener, OnRefreshListener, OnLoadMoreListener {
	private final static String TAG = "MessageActivity";

	private final static int LOADER_ID = 203;

	private MessagePresenter mPresenter;

	public IRecyclerView mRecyclerView;
	public WrapperAdapter mWrapAdapter;
	private MultiItemTypeAdapter<MessageModel.Message> mAdapter;

	private List<MessageModel.Message> mMessageList = new ArrayList<>();
	public int mPageNum = 1;
	public int mPageSize = 4;

	public static void start(Activity aty) {
		Intent i = new Intent(aty, MessageActivity.class);
		aty.startActivity(i);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle bundle) {
		return new PresenterLoader<>(this, new MessagePresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mPresenter = (MessagePresenter) data;
		mPresenter.attach(this);
		onRefresh();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);
		setTitle(R.string.title_activity_message);
		StatService.onEvent(this, StatIDSet.EVENT_ID_CHECK_STATION_LETTER);

		mRecyclerView = (IRecyclerView) findViewById(R.id.recycler_view);
		mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

		mAdapter = new CommonAdapter<MessageModel.Message>(R.layout.message_list_item, null, mRecyclerView.getItemViewDelegateManager()) {
			@Override
			protected void convert(ViewHolder holder, MessageModel.Message message, int position) {
				holder.setText(R.id.msg_title, message.title);
				holder.setText(R.id.msg_date, message.created);
				holder.setText(R.id.msg_content, message.content);
			}
		};
		mWrapAdapter = new WrapperAdapter(mAdapter, mRecyclerView, this, R.layout.state_view_no_message, R.layout.include_view_no_network);
		mWrapAdapter.setRetryClickListener(this);
		mRecyclerView.setAdapter(mWrapAdapter);

		mRecyclerView.setLoadMoreEnabled(false);//第一版产品说不用分页加载
		mRecyclerView.setRefreshEnabled(true);
		mRecyclerView.setOnRefreshListener(this);
		mRecyclerView.setOnLoadMoreListener(this);
		mRecyclerView.setLoadMoreViewStatus(FooterLoadMoreView.Status.GONE);

		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public void onRetry() {
		CLog.i(TAG, "onRetry");
		mRecyclerView.setRefreshing(true);
	}

	@Override
	public void onRefresh() {
		CLog.i(TAG, "onRefresh");
		requestData(true);
	}

	@Override
	public void onLoadMore() {
		CLog.i(TAG, "onLoadMore");
		// 如果当前页有mPageSize条数据，则从后台加载数据
		if ((mAdapter.getCount() % mPageSize) == 0) {
			requestData(false);
		} else {
			showToast(R.string.no_more_data);
			mRecyclerView.hideFooterLoadMoreView();
		}
	}

	private void requestData(boolean isRefresh) {
		mPageNum = isRefresh ? 1 : ++mPageNum;
		mPresenter.getPageList(mPageNum, mPageSize);
	}

	@Override
	public void onPageListSuccess(boolean isRefresh, List<MessageModel.Message> list) {
		if (isRefresh) {
			mRecyclerView.stopRefreshing();
			mRecyclerView.hideFooterLoadMoreView();
			mWrapAdapter.refreshData(list);
		} else {
			mRecyclerView.hideFooterLoadMoreView();
			mWrapAdapter.loadData(list);
		}
	}

	@Override
	public void onPageListFailed(String tips) {
		mPageNum--;
		mRecyclerView.stopRefreshing();
		mRecyclerView.hideFooterLoadMoreView();
		showErrorIfFirstPage();
	}

	@Override
	public void onFunctionalError(Throwable e, boolean autoTips, int functionalCode) {
		super.onFunctionalError(e, autoTips, functionalCode);
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mRecyclerView.hideFooterLoadMoreView();
				mRecyclerView.stopRefreshing();
				showErrorIfFirstPage();
			}
		});
	}

	private void showErrorIfFirstPage() {
		if (mPageNum <= 1) {
			mWrapAdapter.setState(StatesAdapter.STATE_ERROR);
		}
	}
}
